<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="All Rights Reserved, Copyright (C) 2013, Wuyeguo, Ltd." />
<title>EasyUI Web Admin Power by Wuyeguo</title>
<link rel="stylesheet" type="text/css" href="../easyui/1.5.3/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>
<script type="text/javascript" src="../xheditor/xheditor-1.2.2.min.js"></script>
<script type="text/javascript" src="../xheditor/xheditor_lang/zh-cn.js"></script>
</head>
<body>
	<input type="text" name="" id="inp" value="" style="display:none" />
	 <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'west',split:true" title="West" style="width:200px;">
				<ul id="tt"></ul>
				
        </div>
        <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'">
        		<!--<a href="javascript:void(0)" onclick="addTab('评论管理模块','temp/comment.html',null,1)">评论页</a>-->
  				<textarea name="" rows="20" cols="145" class="xheditor"></textarea>
        		<table id="dg"></table>
        
        </div>
    </div>
    <div id="user-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
        <form id="user-form" class="easyui-form" method="post" data-options="novalidate:true">
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="title" style="width:100%" data-options="label:'Name:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="subhead" style="width:100%" data-options="label:'Subject:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="content" style="width:100%" data-options="label:'Subject:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="author" style="width:100%;height:60px" data-options="label:'Message:',multiline:true">
            </div>
            
        </form>
   
	</div>
    <script>
    	
    	
    	function addTab(title, href, iconCls, iframe){
			var tabPanel = parent.$('#wu-tabs');
			if(!tabPanel.tabs('exists',title)){
				var content = '<iframe scrolling="auto" frameborder="0"  src="'+ href +'" style="width:100%;height:100%;"></iframe>';
				if(iframe){
					tabPanel.tabs('add',{
						title:title,
						content:content,
						iconCls:iconCls,
						fit:true,
						cls:'pd3',
						closable:true
					});
				}
				else{
					tabPanel.tabs('add',{
						title:title,
						href:href,
						iconCls:iconCls,
						fit:true,
						cls:'pd3',
						closable:true
					});
				}
			}
			else
			{
				tabPanel.tabs('select',title);
				var current_tab = tabPanel.tabs('getSelected');
                  tabPanel.tabs('update',{
                       tab:current_tab,
                       options : {
                            content : '<iframe scrolling="auto" frameborder="0"  src="'+href+'" style="width:100%;height:100%;"></iframe>',
                      //或者 href : '';
                       }
                  });
			}
		}
   

    	$('#tt').tree({
    		animate:true,
		    url:'http://localhost:3000/cate/list',
		    method:'get',
		    onClick: function(node){
		    	initList(node._id)
			}
		});
		
		
		function initList(cateId){
			var postUrl;
			if(cateId!=undefined && cateId.trim().length>0){
				queryParams = {cateId:cateId}
			}else{
				queryParams = {};
			}
			
			$('#dg').datagrid({
				url:'http://localhost:3000/news/list',
				method:'post',
				pagination:true,
//				fit:true,
				pageSize:2,
				queryParams:queryParams,
				pageList:[2,4,6,8,10],
				columns:[[
					{field:'checkbox',checkbox:true},
			        {field:'title',title:'标题',width:100,align:'center'},
			        {field:'subhead',title:'副标题',width:100,align:'center'},
			        {field:'content',title:'内容',width:180,align:'center'},
			        {field:'author',title:'作者',width:100,align:'center'},
			        {field:'date',title:'时间',width:200,align:'center'},
			        {field:'编辑',title:'编辑',width:170,align:'center',
			        formatter: function(value,row,index){
						return '<a href="javascript:void(0)" style="display:inline-block;padding:0px 5px;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);border-radius: 5px 5px 5px 5px" onclick="editUser(\''+ row._id+'\')">修改</a> '+
						'<a href="javascript:void(0)" style="display:inline-block;padding:0px 5px;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);border-radius: 5px 5px 5px 5px" onclick="deleUser(\''+ row._id+'\')">删除 </a>' + 
						'<a href="javascript:void(0)" style="display:inline-block;padding:0px 5px;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);border-radius: 5px 5px 5px 5px" onclick="addTab('+"'评论管理模块'"+','+"'temp/comment.html?id="+row._id+"'"+',null,1)"> 查看评论详情</a>'
						}}
			    ]]
			})
		
		}
		
		
		initList();
		
		function deleUser(id){
			console.log(id)
			$.ajax({
				type:"delete",
				url:"http://localhost:3000/news/data/"+id,
				async:true
			}).then(function(res){
				console.log(res)
				$('#dg').datagrid('reload');
			})
		}
		
		
		function editUser(id){
			$('#inp').val(id);
			console.log($('#inp').val())
			$('#user-form').form('clear');
			$('#user-dialog').dialog({
				closed: false,
				modal:true,
	            title: "修改信息",
	            buttons: [{
	                text: '确定',
	                iconCls: 'icon-ok',
	                handler: userEditAction
	            }, {
	                text: '取消',
	                iconCls: 'icon-cancel',
	                handler: function () {
	                    $('#user-dialog').dialog('close');                    
	                }
	            }]
	        });
		}
		
		
		function userEditAction(){
			var id = $('#inp').val()
			$('#user-form').form('submit',{
	                onSubmit:function(){
	                	console.log($('#user-form').serializeJSON());
	                	

	                    if($(this).form('enableValidation').form('validate')){
	                    	$.ajax({
	                    		type:"post",
	                    		url:"http://localhost:3000/news/data/"+id,
	                    		data:$('#user-form').serializeJSON()
	                    	}).then(function(res){
//	                    		console.log(res)
	                    	})
	                    	$.messager.alert('信息提示','修改成功！','info');
							$('#user-dialog').dialog('close');
							$('#dg').datagrid('reload');
	                    }else{
	                    	alert('no')
	                    }
	                    
	                }
	               
	         });
		}
	    </script>
</body>
</html>

